<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 保障对象统计报表管理
 * Class ReportRentalController
 * @package Admin\Controller
 */
class ReportRepairController extends BaseController {


    protected function fn_GetLastYears($num=5){
        $years = array();
        $year = intval(date("Y"));
        for($i=-1; $i<$num; $i++){
            $years[] = $year-$i;
        }
        return $years;
    }

    public function ajax_export(){

        header("Content-type: text/html; charset=utf-8");
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=维修统计报表.xls");  //文件下载

        $houseSt = array();
        $total = array("num"=>0, "size"=>0, "total"=>0);
        $year = I("request.year")?I("request.year"):"all";
        $this->action_GenerateReport($year, $houseSt, $total);

        echo '<table width="200" border="1">
                  <tr>
                    <td>统计小区</td>
                    <td>人工费用</td>
                    <td>材料费用</td>
                    <td>其他费用</td>
                    <td>总共维修金额</td>
                  </tr>';
        foreach($houseSt as $item){
            $tr = "
                <tr>
                <td>{{name}}</td>
                <td>{{wx1}}</td>
                <td>{{wx2}}</td>
                <td>{{wx3}}</td>
                <td>{{total}}</td>
              </tr>";
            echo  strtr($tr, [
                "{{name}}" => $item['name'],
                "{{wx1}}" => $item['wx1'],
                "{{wx2}}" => $item['wx2'],
                "{{wx3}}" => $item['wx3'],
                "{{total}}" => $item['total'],
            ]);
        }
        $footer = "
                <tr>
                <td>总计：</td>
                <td>{{wx1}}</td>
                <td>{{wx2}}</td>
                <td>{{wx3}}</td>
                <td>{{total}}</td>
              </tr>
            </table>";
        echo  strtr($footer, ["{{wx1}}" => $total['wx1'],"{{wx2}}" => $total['wx2'],"{{wx3}}" => $total['wx3'],"{{total}}" => $total['total']]);
    }

    private function action_GenerateReport($year, &$houseSt, &$total)
    {
        $where = array('wxzt'=>'维修完成');
        if(I("post.year")!="all"){
            $year1 = $year."/01/01 00:00:00";
            $year2 = $year."/12/31 24:00:00";
            $where["r.yanshou_fdate"] = array('between',array($year1, $year2));
        }
        $houseSt = array();
        $community = M("community")->field("name")->select();
        foreach($community as $value){
            $houseSt[$value['name']] = array("name"=>$value['name'],"wx1"=>0,"wx2"=>0,"wx3"=>0,"total"=>0);
        }

        $data = M("repair r")->where($where)
            ->field("c.name, sum(r.yanshou_rgf) as wx1, sum(r.yanshou_clf) as wx2,
            sum(r.yanshou_qtf) as wx3, sum(r.yanshou_total) as total")
            ->join(C("DB_PREFIX")."community c on c.id = r.community_id", 'LEFT')
            ->group("c.name")
            ->select();
        //echo M()->getLastSql();
        foreach ($data as $item) {
            $houseSt[$item['name']]['wx1'] = $item['wx1'];
            $houseSt[$item['name']]['wx2'] = $item['wx2'];
            $houseSt[$item['name']]['wx3'] = $item['wx3'];
            $houseSt[$item['name']]['total'] = $item['total'];
            $total['wx1'] += $item['wx1'];
            $total['wx2'] += $item['wx2'];
            $total['wx3'] += $item['wx3'];
            $total['total'] += $item['total'];
        }
        $houseSt = array_values($houseSt);
    }

        /**
     * 保障对象统计报表管理 -> formview
     */
    public function detail(){
        $years = $this->fn_GetLastYears();
        $this->assign("years", $years);
        $year = I("post.year")?I("post.year"):date("Y");
        $this->assign("year", $year);

        $houseSt = array();
        $total = array("wx1"=>0, "wx2"=>0, "wx3"=>0, "total"=>0);
        $this->action_GenerateReport($year, $houseSt, $total);
        $this->assign("total", $total);
        $this->assign("rental", $houseSt);
        $this->display();
    }


}